package org.tang.springcloudvv.auth.service;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.tang.springcloudvv.vo.UserDetailsImpl;

import java.util.HashMap;
import java.util.Map;

/**
 * <p><em>Created on 2022/3/28 3:31 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    public static Map<String,UserDetails> mapUsers = new HashMap<>();
    static {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        for (int i = 0; i < 10; i++) {
            String username = "tang"+i;
            UserDetailsImpl userDetails = new UserDetailsImpl();
            userDetails.setUsername(username);
            //userDetails.setPassword("123456");
            //密码加密存储
            userDetails.setPassword(bCryptPasswordEncoder.encode("123456"));
            mapUsers.put(username,userDetails);
        }
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        return mapUsers.get(username);
    }
}
